package psbc.daily.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import psbc.daily.entity.Category;
import psbc.daily.entity.MyLog;
import psbc.daily.mapper.CategoryMapper;
import psbc.daily.mapper.MyLogMapper;
import psbc.daily.service.CategoryService;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private MyLogMapper myLogMapper;
    @Transactional
    @Override
    public int delById(int categoryId) {
        QueryWrapper<MyLog> myLogQueryWrapper = new QueryWrapper<>();
        myLogQueryWrapper.eq("category_id", categoryId);
        List<MyLog> myLogs = myLogMapper.selectList(myLogQueryWrapper);
        if (myLogs.size() == 0){
            return categoryMapper.deleteById(categoryId);
        }
        return 0;
    }

    @Override
    public int insert(Category category) {
        return categoryMapper.insert(category);
    }
}
